import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { PatrolStateType } from './typings'
import {
  getPatrolPlanListGet,
  getPlancreatedToday,
  postAddAreaListSelect,
  postAddPatrolsPlan
} from '~/api/oam/patrol'
import { filterValueEnum } from '~/utils/util'

const patrolStatusSelectList = [
  { value: 0, label: '未开始' },
  { value: 1, label: '进行中' },
  { value: 2, label: '已完成' },
  { value: 3, label: '超时' }
]

const patrol: Module<PatrolStateType, PatrolStateType> = {
  namespaced: true,
  state: {
    patrolStatusSelectList, // 巡视 完成情况
    patrolStatusEnum: filterValueEnum(
      patrolStatusSelectList,
      'value',
      'label'
    ) as PatrolStateType['patrolStatusEnum']
  },
  getters: {
    patrolStatusSelectList: (state) => state.patrolStatusSelectList, // 完成情况
    patrolStatusEnum: (state) => state.patrolStatusEnum
  },
  mutations: {},
  actions: {
    // 巡视任务(计划) 列表
    GetPatrolPlanListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPatrolPlanListGet<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 巡视任务->今日新增
    GetPlancreatedToday<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPlancreatedToday<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 新增台区
    PostAddAreaListSelect<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postAddAreaListSelect<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 新增巡视计划
    PostAddPatrolsPlan<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postAddPatrolsPlan<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default patrol
